# file = open("./file/text.txt","r",encoding="utf-8")
# print(file.readline(),end='')
# print(file.readline(),end='')
# print(file.readline(),end='')

# with open("./file/lc.txt","r",encoding="utf-8")as f:
#     a=f.read()
# print(a.count('python'))

# with open("./file/lc.txt","r",encoding="utf-8")as f:
#     res=f.read()
# print(res)
# count=0
# str_list=res.split("\n")
# for i in str_list:
#     count+=1
# print(f"总行数{count}")

# f = open("./file/text.txt","r",encoding="utf-8")
# str1 = f.read()
# str2 = str1.replace('222','666')
# f.close()
# print(str2)
# fr = open("./file/text.txt","w",encoding="utf-8")
# fr.write(str2)
# fr.flush()
# fb = open("./file/text.txt","r",encoding="utf-8")
# print(fb.read())

# with open('./file/2file.txt','w',encoding='utf-8') as f:
#     s=("2023-10-01 晴\n今天学习了Python的文件操作")
#     f.write(s)
# print("日记写入完成")

# date = input("请输入今天的日期:")
# weather = input("记录天气:")
# text = input("请编写日记:")
# with open('./file/3file.txt','w',encoding='utf-8') as f:
#     f.write(f"{date} {weather}\n")
#     # text.replace(",","\n")
#     f.write(f"{text.replace("，","\n")}")

# # 读取bil.txt中的内容
# f = open('./files/bil.txt','r',encoding='utf-8')
# res = f.read()
# f.close()
# # 写入bil.txt.bak中
# f_bak = open('./files/bil.txt.bak','w',encoding='utf-8')
# f_bak.write(res)
# f_bak.flush()
# 老的方法

with open('./file/bil.txt','r+',encoding='utf-8') as f,open('./file/bil.txt.bak','w',encoding='utf-8') as f_bak:
    # 备份
    res = f.read()
    f_bak.write(res)

    # 内容删除
    s = ""   
    for line in res.split('\n'):
        # 如果找不到返回-1
        if line.find('测试') == -1:
            s += line + '\n'
    # 回转指针
    f.seek(0)
    # 清空内容
    f.truncate()
    # 写入新内容
    f.write(s)